Skip to content

Refactor: Extract generic field parser for update entity configs#7052

Merged
pelikhan merged 2 commits intomainfrom
copilot/refactor-entity-config-parsing
Dec 20, 2025
Merged

Refactor: Extract generic field parser for update entity configs#7052
pelikhan merged 2 commits intomainfrom
copilot/refactor-entity-config-parsing

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 20, 2025

Field parsing for update-issue, update-discussion, and update-pull-request was duplicated across three files with near-identical code blocks (~54 lines total). This created maintenance burden and risk of behavioral drift.

Changes

  • Added parseUpdateEntityBoolField helper in update_entity_helpers.go

    • Supports two parsing modes: key-existence (issues/discussions) vs bool-value (pull requests)
    • Consolidates field parsing logic into single implementation
  • Refactored three parsers to use the helper:

    • update_issue.go: 25 lines → 3 lines
    • update_discussion.go: 23 lines → 3 lines
    • update_pull_request.go: 21 lines → 2 lines
  • Added comprehensive test coverage in update_entity_helpers_test.go

    • Covers both parsing modes
    • Tests nil configs, missing keys, type coercion

Before/After

// Before: Repeated across 3 files
if configMap != nil {
    if _, exists := configMap["status"]; exists {
        updateIssuesConfig.Status = new(bool)
    }
    if _, exists := configMap["title"]; exists {
        updateIssuesConfig.Title = new(bool)
    }
    // ... more repetition
}

// After: Single helper call
updateIssuesConfig.Status = parseUpdateEntityBoolField(configMap, "status", FieldParsingKeyExistence)
updateIssuesConfig.Title = parseUpdateEntityBoolField(configMap, "title", FieldParsingKeyExistence)

No functional changes. All existing tests pass.

Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] Update entity config parsing duplicated across update-issue/discussion/PR</issue_title>
<issue_description># 🔍 Duplicate Code Detected: Update entity config parsing duplicated across update-issue/discussion/PR

Analysis of commit be57766

Assignee: @copilot

Summary

The parsing logic for safe-output update jobs is copy-pasted across issue, discussion, and pull-request compilers. Each file builds the same parseUpdateEntityConfig call and re-parses per-field booleans in nearly identical blocks. This invites drift in validation defaults and logging.

Duplication Details

Pattern: Repeated update entity parsing scaffolding

  • Severity: Medium
  • Occurrences: 3
  • Locations:
    • pkg/workflow/update_issue.go:18-60
    • pkg/workflow/update_discussion.go:19-70
    • pkg/workflow/update_pull_request.go:17-60
  • Code Sample:
    params := UpdateEntityJobParams{EntityType: UpdateEntityIssue, ConfigKey: "update-issue"}
    parseSpecificFields := func(configMap map[string]any, baseConfig *UpdateEntityConfig) {}
    baseConfig := c.parseUpdateEntityConfig(outputMap, params, updateIssueLog, parseSpecificFields)
    if baseConfig == nil { return nil }
    updateIssuesConfig := &UpdateIssuesConfig{UpdateEntityConfig: *baseConfig}
    if configData, exists := outputMap["update-issue"]; exists {
        if configMap, ok := configData.(map[string]any); ok {
            if _, exists := configMap["title"]; exists { updateIssuesConfig.Title = new(bool) }
            // ...same structure repeats in discussion/PR variants...
        }
    }

Impact Analysis

  • Maintainability: Three near-identical parsers must be edited in lockstep; adding new flags or changing defaults risks inconsistent behavior.
  • Bug Risk: Drift between entities can silently mis-parse fields (e.g., nil vs false handling) and complicate validation fixes.
  • Code Bloat: Repeated scaffolding inflates compiler size with no functional difference.

Refactoring Recommendations

  1. Extract shared parser helper

    • Introduce a helper (e.g., parseUpdateEntityBooleans) that accepts ConfigKey, EntityType, and a map of field handlers (title/body/status/labels) and returns a populated config struct.
    • Estimated effort: 2-3 hours; simplifies adding new update targets.
    • Benefits: Single point for defaults/logging/validation; reduces drift.
  2. Normalize field parsing strategy

    • Define declarative field descriptors (name, default behavior, coercion rules) and iterate rather than branching per file.
    • Estimated effort: 2 hours after helper; improves testability and makes future entities cheaper.

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 6 (focus on update_* compilers and supporting parsers)
  • Detection Method: Serena semantic code analysis
  • Commit: be57766
  • Analysis Date: 2025-02-16

AI generated by Duplicate Code Detector

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Add parseUpdateEntityBoolField helper to eliminate duplicate code
- Support two parsing modes: key existence vs bool value
- Reduces code by ~54 lines with no behavior change
- All existing tests pass

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated entity config parsing in workflow Refactor: Extract generic field parser for update entity configs Dec 20, 2025
Copilot AI requested a review from mnkiefer December 20, 2025 14:10
@pelikhan pelikhan marked this pull request as ready for review December 20, 2025 14:13
@pelikhan pelikhan merged commit 90141fe into main Dec 20, 2025
4 checks passed
@pelikhan pelikhan deleted the copilot/refactor-entity-config-parsing branch December 20, 2025 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Update entity config parsing duplicated across update-issue/discussion/PR

3 participants